package code

import (
	utils "gitee.com/ichub/goconfig/common/base/baseutils/stringutils"
	"gitee.com/ichub/gofactroy/common/ichubbase"
	"gitee.com/ichub/gofactroy/factroy/dbfactroy"
	"strings"
	"time"
)

type ServiceFactroy struct {
}

// ReadTemplateServiceInterface
func (factroy *ServiceFactroy) buildServiceInterface(tableTool *dbfactroy.DbFactroy) string {

	bs := dbfactroy.InstFileFactroy.ReadTemplateServiceInterface()

	vars := make(map[string]interface{})
	vars["FileName"] = utils.Case2Camel(tableTool.Table) + "Interface.go"
	vars["Description"] = "接口" + utils.Case2Camel(tableTool.Table) + "Interface"
	vars["Author"] = tableTool.Author
	vars["DATETIME"] = time.Now().Format(ichubbase.FormatDateTime)

	vars["ModelName"] = utils.Case2Camel(tableTool.Table)
	vars["pkey"] = utils.Case2Camel(tableTool.Pkey)
	vars["pkeyType"] = tableTool.FindGoType(tableTool.PkeyType)

	return utils.ParseTemplateString(string(bs), vars)

}

func (factroy *ServiceFactroy) BuildESServiceFile(dbt *dbfactroy.DbFactroy) (d, f, c string) {

	fbase := utils.Lcfirst(utils.Case2Camel(dbt.Table))
	d = dbfactroy.InstFileFactroy.TransDir(dbfactroy.Gendir_esservice, fbase)

	f, c = fbase+"ESService.go", factroy.buildService(dbt, true)
	dbfactroy.InstFileFactroy.WriteFileModule(d, f, c)

	return
}

func (factroy *ServiceFactroy) BuildInterfaceFile(dbt *dbfactroy.DbFactroy) (sdir, f, c string) {

	fbase := utils.Lcfirst(utils.Case2Camel(dbt.Table))
	sdir = dbfactroy.InstFileFactroy.TransDir(dbfactroy.Gendir_interface, fbase)

	f, c = fbase+"Interface.go", factroy.buildServiceInterface(dbt)
	dbfactroy.InstFileFactroy.WriteFileModule(sdir, f, c)

	return
}

func (factroy *ServiceFactroy) buildESService(tableTool *dbfactroy.DbFactroy) string {

	bs := dbfactroy.InstFileFactroy.ReadTemplateService()

	vars := make(map[string]interface{})
	vars["ModelName"] = "ES" + utils.Case2Camel(tableTool.Table)
	vars["pkey"] = utils.Case2Camel(tableTool.Pkey)
	vars["pkeyType"] = tableTool.FindProtoType(tableTool.PkeyType)
	s := string(bs)
	s = strings.Replace(s, "service", "esservice", 0)

	return utils.ParseTemplateString(s, vars)

}

func (factroy *ServiceFactroy) buildService(dbt *dbfactroy.DbFactroy, buildEs bool) string {
	bs := dbfactroy.InstFileFactroy.ReadTemplateService()

	vars := make(map[string]interface{})

	vars["FileName"] = utils.Case2Camel(dbt.Table) + "Service.go"
	vars["Description"] = "服务" + utils.Case2Camel(dbt.Table) + "Service"
	vars["Author"] = dbt.Author
	vars["DATETIME"] = time.Now().Format(ichubbase.FormatDateTime)

	vars["ModelName"] = utils.Case2Camel(dbt.Table)
	vars["ESModelName"] = utils.Case2Camel(dbt.Table)
	if buildEs {
		vars["ESModelName"] = "ES" + vars["ESModelName"].(string)
	}
	vars["pkey"] = utils.Case2Camel(dbt.Pkey)
	vars["pkeyType"] = dbt.FindGoType(dbt.PkeyType)
	vars["PkeyValue"] = "0"
	vars["fmtType"] = "%d"
	if dbt.FindGoType(dbt.PkeyType) == "string" {
		vars["PkeyValue"] = "\"\""
		vars["fmtType"] = "%s"
	}
	s := string(bs)
	if !buildEs {
		s = strings.Replace(s, "esservice", "service", 1)
	}
	return utils.ParseTemplateString(s, vars)

}

func (factroy *ServiceFactroy) BuildServiceFile(dbt *dbfactroy.DbFactroy) (d, f, c string) {

	fbase := utils.Lcfirst(utils.Case2Camel(dbt.Table))
	d = dbfactroy.InstFileFactroy.TransDir(dbfactroy.Gendir_service, fbase)

	f, c = fbase+"Service.go", factroy.buildService(dbt, false)
	dbfactroy.InstFileFactroy.WriteFileModule(d, f, c)

	return d, f, c
}

func (factroy *ServiceFactroy) Build(dbt *dbfactroy.DbFactroy) (d, f, c string) {

	factroy.BuildInterfaceFile(dbt)
	factroy.BuildESServiceFile(dbt)

	return factroy.BuildServiceFile(dbt)

}
